package cn.melon.mod.sc;

import java.util.List;

import org.apache.log4j.Logger;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import cn.melon.Helper.LoggerHelper;
import cn.melon.base.BaseController;
import cn.melon.mod.login.UserInfo;

public class ScController extends BaseController {

    public String TAG = LoggerHelper.makeLogTag(ScController.class);
    public Logger scLogger = LoggerHelper.serverLogger();

    public ScController(String request) {
        super(request);
    }

    @Override
    public void onResponse() {

        String req = getRequest();

        scLogger.debug(TAG + req);
        try {
            JSONObject reqJSON = new JSONObject(req);
            String action = reqJSON.getString("action");
            JSONObject jContent = null;

            switch (action) {
                case "ADDONECLASSINFO":
                    ScInfo info = ScInfo.toInfo(reqJSON.getJSONObject("content"));
                    setResponse(addOneClassInfo(info));
                    break;
                case "DELETEONECLASSINFO":
                    String classNumber2 = reqJSON.getString("content");
                    setResponse(deleteOneClassInfo(classNumber2));
                    break;
                case "CHANGEONECLASSINFO":
                    ScInfo info3 = ScInfo.toInfo(reqJSON.getJSONObject("content"));
                    setResponse(changeOneClassInfo(info3.getClassNumber(), info3));
                    break;
                case "ASKALLCLASSINFO":
                    setResponse(askAllClassInfo());
                    break;
                case "ASKONECLASS":
                    String classNumber4 = reqJSON.getString("content");
                    setResponse(askOneClass(classNumber4));
                    break;
                case "ADDONESTUDENTMESSAGE":
                    UserInfo userInfo5 = getUserInfo();
                    String studentNumber5 = userInfo5.getStudentNum();
                    String classNumber5 = reqJSON.getString("content");
                    setResponse(addOneStudentMessage(studentNumber5, classNumber5));
                    break;
                case "DELETEONESTUDENTMESSAGE":
                    UserInfo userInfo6 = getUserInfo();
                    String studentNumber6 = userInfo6.getStudentNum();
                    String classNumber6 = reqJSON.getString("content");
                    setResponse(deleteOneStudentMessage(studentNumber6, classNumber6));
                    break;
                case "ASKONESTUDENTINFO":
                    String studentNumber = reqJSON.getString("content");
                    setResponse(askOneStudentInfo(studentNumber));
                    break;
                case "ASKONESTEDENTALLINFO":
                    UserInfo userInfo7 = getUserInfo();
                    scLogger.debug(TAG + userInfo7.getStudentNum());
                    String studentNumber7 = userInfo7.getStudentNum();
                    setResponse(askOneStudentAllInfo(studentNumber7));
                    break;
                case "DELETEONESTUDENT":
                    String studentNumber9 = reqJSON.getString("content");
                    setResponse(deleteOneStudent(studentNumber9));
                    break;
                case "ADDONESTUDENT":
                    StInfo info10 = StInfo.toInfo(reqJSON.getJSONObject("content"));
                    setResponse(addOneStudentInfo(info10));
                    break;
                case "ASKALLSTUDENT":
                    setResponse(askAllStudent());
                    break;
                default:
                    setResponse("{}");
                    break;
            }

        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    /**
     * 添加一门课程信息（管理员）
     * @param info1 课程信息
     * @return JSON 字符串
     */
    public String addOneClassInfo(ScInfo info1) {
        UserInfo userInfo = getUserInfo();
        JSONObject jRet = new JSONObject();

        IScDao scDao = new IScDaolmpl();

//        if (userInfo.getType().equals("student")) {
//            try {
//                jRet.put("code", 300);
//                jRet.put("content", "该用户无此权限");
//            } catch (JSONException e) {
//                e.printStackTrace();
//            }
//            return jRet.toString();
//        }

        if (info1 == null) {
            try {
                jRet.put("code", 300);
                jRet.put("content", "客户端信息错误");
            } catch (JSONException e) {
                e.printStackTrace();
            }

            return jRet.toString();
        }

        if (scDao.addInfo(info1)) {
                try {
                    jRet.put("code", 200);
                    jRet.put("content", "更新信息成功");
                } catch (JSONException e) {
                    e.printStackTrace();
                }
        } else {
            try {
                jRet.put("code", 300);
                jRet.put("content", "更新信息失败");
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }

        return jRet.toString();

    }

    /**
     * 删除一门课程信息（管理员）
     * @param classId2 要删除的课程号码
     * @return json字符串
     */
    public String deleteOneClassInfo(String classId2) {
        UserInfo userInfo = getUserInfo();
        JSONObject jRet = new JSONObject();

        IScDao scDao = new IScDaolmpl();

        if (userInfo.getType().equals("student")) {
            try {
                jRet.put("code", 300);
                jRet.put("content", "该用户无此权限");
            } catch (JSONException e) {
                e.printStackTrace();
            }
            return jRet.toString();
        }
        if (classId2 == null) {
            try {
                jRet.put("code", 300);
                jRet.put("content", "客户端信息错误");
            } catch (JSONException e) {
                e.printStackTrace();
            }

            return jRet.toString();
        }
        if (classId2 != null && classId2 == classId2) {       // 为了底下的else
                                                              // 多加了一个无用的 判断条件
            try {
                scDao.deleteInfo(classId2);
                jRet.put("code", 200);
                jRet.put("content", "更新信息成功");
            } catch (JSONException e) {
                e.printStackTrace();
            }
        } else {
            try {
                jRet.put("code", 300);
                jRet.put("content", "更新信息失败");
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }

        return jRet.toString();

    }

    /**
     * 修改一门课程信息（管理员）
     * @param classId3 要修改课程的课程号码
     * @param info3 要更新的课程信息
     * @return json字符串
     */
    public String changeOneClassInfo(String classId3, ScInfo info3) {
        UserInfo userInfo = getUserInfo();
        JSONObject jRet = new JSONObject();

        IScDao scDao = new IScDaolmpl();

//        TODO 增加权限控制
//        if (userInfo.getType().equals("student")) {
//            try {
//                jRet.put("code", 300);
//                jRet.put("content", "该用户无此权限");
//            } catch (JSONException e) {
//                e.printStackTrace();
//            }
//            return jRet.toString();
//        }
//
        if (classId3 == null || info3 == null) {
            try {
                jRet.put("code", 300);
                jRet.put("content", "客户端信息错误");
            } catch (JSONException e) {
                e.printStackTrace();
            }

            return jRet.toString();
        }

        if (classId3 != null && info3 != null) {
            try {
                scDao.changeInfo(classId3, info3);
                jRet.put("code", 200);
                jRet.put("content", "更新信息成功");
            } catch (JSONException e) {
                e.printStackTrace();
            }
        } else {
            try {
                jRet.put("code", 300);
                jRet.put("content", "更新信息失败");
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }

        return jRet.toString();
    }

    /**
     * 查询所有课程信息
     * @return String
     */
    public String askAllClassInfo() {

        JSONObject jRet = new JSONObject();

        IScDao scDao = new IScDaolmpl();

        List<ScInfo> infoList = scDao.getAllScInfo();

        try {
            if (infoList != null) {
                JSONArray jContent = new JSONArray();
                for (ScInfo info : infoList) {
                    jContent.put(info.toJSON());
                }
                jRet.put("content", jContent);
                jRet.put("code", 200);
            } else {
                jRet.put("code", 300);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return jRet.toString();
    }

    /**
     * 查询一门课程的信息
     * @param classId4 要查询课程的课程号
     * @return String
     */
    public String askOneClass(String classId4) {
        JSONObject jRet = new JSONObject();

        IScDao scDao = new IScDaolmpl();

        ScInfo info = scDao.getInfo(classId4);

        try {
            if (info != null) {

                jRet.put("content", info.toJSON());
                jRet.put("code", 200);
            } else {
                jRet.put("code", 300);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return jRet.toString();
    }

    /**
     * 学生新选了一门课程 添加该课程
     * @param studentNumber5 学生学号
     * @param classNumber5 新选课程的课程号
     * @return String
     */
    public String addOneStudentMessage(String studentNumber5, String classNumber5) {
        UserInfo userInfo = getUserInfo();
        JSONObject jRet = new JSONObject();

        IStDao stDao = new IStDaolmpl();

        if (studentNumber5 == null || classNumber5 == null) {
            try {
                jRet.put("code", 300);
                jRet.put("content", "客户端信息错误");
            } catch (JSONException e) {
                e.printStackTrace();
            }

            return jRet.toString();
        }

        if (studentNumber5 != null && classNumber5 != null) {
            try {
                stDao.addMessage(classNumber5, studentNumber5);
                jRet.put("code", 200);
                jRet.put("content", "更新信息成功");
            } catch (JSONException e) {
                e.printStackTrace();
            }
        } else {
            try {
                jRet.put("code", 300);
                jRet.put("content", "更新信息失败");
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }

        return jRet.toString();
    }

    /**
     * 学生弃选一门课程 删除该课程
     * @param studentNumber6 学生学号
     * @param classNumber6 弃选课程的课程号
     * @return String
     */
    public String deleteOneStudentMessage(String studentNumber6, String classNumber6) {
        JSONObject jRet = new JSONObject();

        IStDao stDao = new IStDaolmpl();

        if (studentNumber6 == null || classNumber6 == null) {
            try {
                jRet.put("code", 300);
                jRet.put("content", "客户端信息错误");
            } catch (JSONException e) {
                e.printStackTrace();
            }

            return jRet.toString();
        }

        if (stDao.deleteMessage(classNumber6, studentNumber6)) {
                try {
                    jRet.put("content", "更新信息成功");
                    jRet.put("code", 200);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
        } else {
            try {
                jRet.put("code", 300);
                jRet.put("content", "更新信息失败");
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }

        return jRet.toString();
    }

    /**
     * 查询一个学生所选的所有课程
     * @param studentNumber7 学生学号
     * @return String
     */
    public String askOneStudentInfo(String studentNumber7) {
        JSONObject jRet = new JSONObject();

        IStDao stDao = new IStDaolmpl();

        StInfo info = stDao.searchOneStudent(studentNumber7);

        try {
            if (info != null) {

                jRet.put("content", info.toJSON());
                jRet.put("code", 200);
            } else {
                jRet.put("code", 300);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return jRet.toString();
    }

    /**
     * 查询一个学生所选的所有课程以及课程的具体信息
     * @param studentNumber8 学生学号
     * @return String
     */
    public String askOneStudentAllInfo(String studentNumber8) {
        JSONObject jRet = new JSONObject();

        IStDao stDao = new IStDaolmpl();

        List<ScInfo> infoList = stDao.getAllInfo(studentNumber8);
        try {
            JSONArray jContent = new JSONArray();
            if (infoList != null  && infoList.size() >= 0) {
                for (ScInfo info : infoList) {
                    jContent.put(info.toJSON());
                }
                jRet.put("content", jContent);
                jRet.put("code", 200);
            }
            else {
                jRet.put("code", 300);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return jRet.toString();
    }

    /**
     * 删除一个学生的选课信息
     * @param studentNumber9 学生学号
     * @return String
     */
    public String deleteOneStudent(String studentNumber9) {
        UserInfo userInfo = getUserInfo();
        JSONObject jRet = new JSONObject();

        IStDao stDao = new IStDaolmpl();

        if (userInfo.getType().equals("student")) {
            try {
                jRet.put("code", 300);
                jRet.put("content", "该用户无此权限");
            } catch (JSONException e) {
                e.printStackTrace();
            }
            return jRet.toString();
        }

        if (studentNumber9 == null) {
            try {
                jRet.put("code", 300);
                jRet.put("content", "客户端信息错误");
            } catch (JSONException e) {
                e.printStackTrace();
            }

            return jRet.toString();
        }

        if (studentNumber9 != null && studentNumber9 != null) {
            try {
                stDao.deleteOneStudent(studentNumber9);
                jRet.put("code", 200);
                jRet.put("content", "更新信息成功");
            } catch (JSONException e) {
                e.printStackTrace();
            }
        } else {
            try {
                jRet.put("code", 300);
                jRet.put("content", "更新信息失败");
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }

        return jRet.toString();
    }

    /**
     * 添加一个学生的选课信息
     * @param Info10 该学生的选课信息
     * @return String
     */
    public String addOneStudentInfo(StInfo Info10) {
        UserInfo userInfo = getUserInfo();
        JSONObject jRet = new JSONObject();

        IStDao stDao = new IStDaolmpl();

        if (userInfo.getType().equals("student")) {
            try {
                jRet.put("code", 300);
                jRet.put("content", "该用户无此权限");
            } catch (JSONException e) {
                e.printStackTrace();
            }
            return jRet.toString();
        }

        if (Info10 == null) {
            try {
                jRet.put("code", 300);
                jRet.put("content", "客户端信息错误");
            } catch (JSONException e) {
                e.printStackTrace();
            }

            return jRet.toString();
        }

        if (Info10 != null && Info10 != null) {
            try {
                stDao.addOneStudent(Info10);
                jRet.put("code", 200);
                jRet.put("content", "更新信息成功");
            } catch (JSONException e) {
                e.printStackTrace();
            }
        } else {
            try {
                jRet.put("code", 300);
                jRet.put("content", "更新信息失败");
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }

        return jRet.toString();
    }

    /**
     * 查询所有学生的选课信息
     * @return String
     */
    public String askAllStudent() {

        JSONObject jRet = new JSONObject();

        IStDao stDao = new IStDaolmpl();

        List<StInfo> infoList = stDao.showAllStudent();

        try {
            if (infoList != null) {
                JSONArray jContent = new JSONArray();
                for (StInfo info : infoList) {
                    jContent.put(info.toJSON());
                }
                jRet.put("content", jContent);
                jRet.put("code", 200);
            } else {
                jRet.put("code", 300);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return jRet.toString();
    }
}